package cn.itsource.itsource.controller;

import cn.itsource.itsource.domain.User;
import cn.itsource.itsource.domain.UserAddress;
import cn.itsource.itsource.service.IUserAddressService;
import cn.itsource.itsource.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/useraddress")
public class UserAddressController {
    @Autowired
    private IUserAddressService userAddressService;
    @Autowired
    private RedisService redisService;

    /**
     * 注意：获取当前登录用户的ID，不是传过来，是根据token到redis中取出来的
     *
     * @return
     */
    @GetMapping
    public List<UserAddress> findAllByUserId(HttpServletRequest request) {
        String token = request.getHeader("token");
        //再根据token到redis中取出登录的用户信息，再从用户信息中取出ID
        User user = redisService.getKeyObjectValue(token, User.class);
        return userAddressService.findAllByUserId(user.getId());
    }
}
